package com.groupseven.backend.services.impl;

import com.groupseven.backend.domain.Echarts.EchartsData;
import com.groupseven.backend.domain.Echarts.EchartsDict;
import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.mapper.EchartsMapper;
import com.groupseven.backend.services.EchartsService;
import com.groupseven.backend.services.UserService;
import com.groupseven.backend.test.DataEncode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class EchartsServiceImpl implements EchartsService {
    @Autowired
    private EchartsMapper echartsmapper;
    @Autowired
    private UserService userService;
    @Autowired
    private DataEncode dataEncode;

    public Map<String,List<String>> getTeacherQuantityAndLevel(String teacher){
        Map<String,List<String>> result = new HashMap<>();
        List<String> Quantity = new ArrayList<>();
        List<String> Programlevel = new ArrayList<>();
        List<String> Programquantity = new ArrayList<>();
        List<String> Paperlevel = new ArrayList<>();
        List<String> Paperquantity = new ArrayList<>();
        List<String> Patentlevel = new ArrayList<>();
        List<String> Patentquantity = new ArrayList<>();
        List<String> Awardlevel = new ArrayList<>();
        List<String> Awardquantity = new ArrayList<>();
        List<EchartsDict> Level = echartsmapper.getLevelDict();
        List<EchartsData> Programs = echartsmapper.getProgramsQuantityByUsername();
        List<EchartsData> Papers = echartsmapper.getPapersQuantityByUsername();
        List<EchartsData> Patents = echartsmapper.getPatentsQuantityByUsername();
        List<EchartsData> Awards = echartsmapper.getAwardsQuantityByUsername();
        for(EchartsData program : Programs){
            if(program.getxAxis().equals(teacher)){
                for(EchartsDict level : Level){
                    if(level.getValue()==program.getLevel()){
                        Programlevel.add(level.getKey());
                        Programquantity.add(program.getyAxis()+"");
                    }
                }
            }
        }
        for(EchartsData paper : Papers){
            if(paper.getxAxis().equals(teacher)){
                for(EchartsDict level : Level){
                    if(level.getValue()==paper.getLevel()){
                        Paperlevel.add(level.getKey());
                        Paperquantity.add(paper.getyAxis()+"");
                    }
                }
            }
        }
        for(EchartsData patent : Patents){
            if(patent.getxAxis().equals(teacher)){
                for(EchartsDict level : Level){
                    if(level.getValue()==patent.getLevel()){
                        Patentlevel.add(level.getKey());
                        Patentquantity.add(patent.getyAxis()+"");
                    }
                }
            }
        }
        for(EchartsData award : Awards){
            if(award.getxAxis().equals(teacher)){
                for(EchartsDict level : Level){
                    if(level.getValue()==award.getLevel()){
                        Awardlevel.add(level.getKey());
                        Awardquantity.add(award.getyAxis()+"");
                    }
                }
            }
        }
        result.put("programlevel",Programlevel);
        result.put("programquantity",Programquantity);
        result.put("paperlevel",Paperlevel);
        result.put("paperquantity",Paperquantity);
        result.put("patentlevel",Patentlevel);
        result.put("patentquantity",Patentquantity);
        result.put("awardlevel",Awardlevel);
        result.put("awardquantity",Awardquantity);
        return result;
    }

    public Map<String,List<String>> getQuantityByTeacherName(String teacher){
        Map<String,List<String>> result = new HashMap<>();
        List<User> users = userService.getAllUsers();
        List<String> usernames = new ArrayList<>();
        List<String> Quantity = new ArrayList<>();
        List<String> Programquantity = new ArrayList<>();
        List<String> Paperquantity = new ArrayList<>();
        List<String> Patentquantity = new ArrayList<>();
        List<String> Awardquantity = new ArrayList<>();
        List<String> Users = new ArrayList<>();
        List<EchartsData> Programs = echartsmapper.getProgramsQuantityByUsername();
        List<EchartsData> Papers = echartsmapper.getPapersQuantityByUsername();
        List<EchartsData> Patents = echartsmapper.getPatentsQuantityByUsername();
        List<EchartsData> Awards = echartsmapper.getAwardsQuantityByUsername();
        int programquantity = 0;
        int paperquantity = 0;
        int patentquantity = 0;
        int awardquantity = 0;
        int quantity = 0;
        if(Programs!=null&&Programs.size()!=0){
            for(EchartsData program : Programs){
                if(teacher.equals(program.getxAxis())){
                    programquantity+=program.getyAxis();
                }
            }
        }
        if(Papers!=null&&Papers.size()!=0){
            for(EchartsData paper : Papers){
                if(paper.getxAxis().equals(teacher)){
                    paperquantity+=paper.getyAxis();
                }
            }
        }
        if(Patents!=null&&Patents.size()!=0){
            for(EchartsData patent : Patents){
                if(patent.getxAxis().equals(teacher)){
                    patentquantity+=patent.getyAxis();
                }
            }
        }
        if(Awards!=null&&Awards.size()!=0){
            for(EchartsData award : Awards){
                if(award.getxAxis().equals(teacher)){
                    awardquantity+=award.getyAxis();
                }
            }
        }
        usernames.add(teacher);
        Quantity.add(quantity+"");
        Programquantity.add(programquantity+"");
        Paperquantity.add(paperquantity+"");
        Patentquantity.add(patentquantity+"");
        Awardquantity.add(awardquantity+"");
        result.put("name",usernames);
        result.put("data",Quantity);
        result.put("programdata",Programquantity);
        result.put("paperdata",Paperquantity);
        result.put("patentdata",Patentquantity);
        result.put("awarddata",Awardquantity);
        return result;
    }

    public Map<String,List<String>> getTeachernaneAndQuantity(){
        Map<String,List<String>> result = new HashMap<>();
        List<User> users = userService.getAllUsers();
        List<String> usernames = new ArrayList<>();
        List<String> Quantity = new ArrayList<>();
        List<String> Programquantity = new ArrayList<>();
        List<String> Paperquantity = new ArrayList<>();
        List<String> Patentquantity = new ArrayList<>();
        List<String> Awardquantity = new ArrayList<>();
        List<String> Users = new ArrayList<>();
        List<EchartsData> Programs = echartsmapper.getProgramsQuantityByUsername();
        List<EchartsData> Papers = echartsmapper.getPapersQuantityByUsername();
        List<EchartsData> Patents = echartsmapper.getPatentsQuantityByUsername();
        List<EchartsData> Awards = echartsmapper.getAwardsQuantityByUsername();
        for(User user : users){
            user.setUsername(dataEncode.decodeBae64(user.getUsername()));
        }
        for(User user : users) {
            System.out.println(user.getUsername());
            try {
                int programquantity = 0;
                int paperquantity = 0;
                int patentquantity = 0;
                int awardquantity = 0;
                int quantity = 0;
                if(Programs==null||Programs.size()==0) {

                }else {
                    for (EchartsData program : Programs) {
                        if(program.getxAxis().equals(user.getUsername())){
                            programquantity += program.getyAxis();
                        }
                    }
                }
                if(Papers==null||Papers.size()==0) {

                } else {
                    for (EchartsData paper : Papers) {
                        if(paper.getxAxis().equals(user.getUsername())){
                            paperquantity += paper.getyAxis();
                        }
                    }
                }
                if(Patents==null||Patents.size()==0){

                } else {
                    for (EchartsData patent : Patents) {
                        if(patent.getxAxis().equals(user.getUsername())){
                            patentquantity += patent.getyAxis();
                        }
                    }
                }
                if(Awards==null||Awards.size()==0){

                } else {
                    for (EchartsData award : Awards) {
                        if(award.getxAxis().equals(user.getUsername())){
                            awardquantity += award.getyAxis();
                        }
                    }
                }
                quantity += awardquantity + paperquantity + programquantity + patentquantity;
                Quantity.add(quantity + "");
                Programquantity.add(programquantity + "");
                Paperquantity.add(paperquantity + "");
                Patentquantity.add(patentquantity + "");
                Awardquantity.add(awardquantity + "");
                Users.add(user.getUsername());
            } catch (Exception e){
                e.printStackTrace();
                System.out.println(user.getUsername()+"发生了异常");
                continue;
            }
        }
        result.put("name",Users);
        result.put("data",Quantity);
        result.put("programdata",Programquantity);
        result.put("paperdata",Paperquantity);
        result.put("patentdata",Patentquantity);
        result.put("awarddata",Awardquantity);
        return result;
    }

    public Map<String,List<String>> getAffiliationAndQuantity(){
        List<String> Affiliations = echartsmapper.getAffiliations();
        Map<String,List<String>> result = new HashMap<>();
        List<EchartsData> Programs = echartsmapper.getProgramsQuantityOrderByAffliation();
        List<EchartsData> Papers = echartsmapper.getPapersQuantityOrderByAffliation();
        List<EchartsData> Patents = echartsmapper.getPatentsQuantityOrderByAffliation();
        List<EchartsData> Awards = echartsmapper.getAwardsQuantityOrderByAffliation();
        List<String> quantity = new ArrayList<>();
        List<String> programquantity = new ArrayList<>();
        List<String> paperquantity = new ArrayList<>();
        List<String> patentquantity = new ArrayList<>();
        List<String> awardquantity = new ArrayList<>();
        int programcount = 0;
        int papercount = 0;
        int patentcount = 0;
        int awardcount = 0;
        int affiliationcount = 0;
        for(String affiliationIterator : Affiliations){
            affiliationcount++;
            int Quantity = 0;
            int ProgramQuantity = 0;
            int PaperQuantity = 0;
            int PatentQuantity = 0;
            int AwardQuantity = 0;
            for(EchartsData program : Programs){
                if(program.getxAxis().equals(affiliationIterator)){
                    Quantity += program.getyAxis();
                    ProgramQuantity += program.getyAxis();
                    programcount++;
                }
            }
            for(EchartsData paper : Papers){
                if(paper.getxAxis().equals(affiliationIterator)){
                    Quantity += paper.getyAxis();
                    PaperQuantity += paper.getyAxis();
                    papercount++;
                }
            }
            for(EchartsData patent : Patents){
                if(patent.getxAxis().equals(affiliationIterator)){
                    Quantity += patent.getyAxis();
                    PatentQuantity += patent.getyAxis();
                    patentcount++;
                }
            }
            for(EchartsData award : Awards){
                if(award.getxAxis().equals(affiliationIterator)){
                    Quantity += award.getyAxis();
                    AwardQuantity += award.getyAxis();
                    awardcount++;
                }
            }
            programquantity.add(ProgramQuantity+"");
            paperquantity.add(PaperQuantity+"");
            patentquantity.add(PatentQuantity+"");
            awardquantity.add(AwardQuantity+"");
            quantity.add(Quantity+"");
        }
        System.out.println("单位执行了"+affiliationcount+"次");
        System.out.println("论文执行了"+papercount+"次");
        System.out.println("项目执行了"+programcount+"次");
        System.out.println("专利执行了"+patentcount+"次");
        System.out.println("获奖执行了"+awardcount+"次");
        result.put("paperdata",paperquantity);
        result.put("patentdata",patentquantity);
        result.put("programdata",programquantity);
        result.put("awarddata",awardquantity);
        result.put("unit",Affiliations);
        result.put("data",quantity);
        return result;
    }

    public Map<String,Integer> getEchartsDataOfAffiliationAndQuantity(){
        List<String> Affiliations = echartsmapper.getAffiliations();
        Map<String,Integer> result = new HashMap<>();
        List<EchartsData> Programs = echartsmapper.getProgramsQuantityOrderByAffliation();
        List<EchartsData> Papers = echartsmapper.getPapersQuantityOrderByAffliation();
        List<EchartsData> Patents = echartsmapper.getPatentsQuantityOrderByAffliation();
        List<EchartsData> Awards = echartsmapper.getAwardsQuantityOrderByAffliation();
        for(String affiliationIterator : Affiliations){
            int Quantity = 0;
            for(EchartsData program : Programs){
                if(program.getxAxis().equals(affiliationIterator)){
                    Quantity += program.getyAxis();
                }
            }
            for(EchartsData paper : Papers){
                if(paper.getxAxis().equals(affiliationIterator)){
                    Quantity += paper.getyAxis();
                }
            }
            for(EchartsData patent : Patents){
                if(patent.getxAxis().equals(affiliationIterator)){
                    Quantity += patent.getyAxis();
                }
            }
            for(EchartsData award : Awards){
                if(award.getxAxis().equals(affiliationIterator)){
                    Quantity += award.getyAxis();
                }
            }
            result.put(affiliationIterator,Quantity);
        }
        return result;
    }

    public Map<String,Map<String,Map<String,Integer>>> getEchartDataOfAffiliationAndQuantityAndLevel(){
        List<EchartsData> programs = echartsmapper.getProgramsQuantityOrderByAffliation();
        List<EchartsData> papers = echartsmapper.getPapersQuantityOrderByAffliation();
        List<EchartsData> patents = echartsmapper.getPatentsQuantityOrderByAffliation();
        List<EchartsData> awards = echartsmapper.getAwardsQuantityOrderByAffliation();
        List<EchartsDict> leveldict = echartsmapper.getLevelDict();
        List<String> affiliations = echartsmapper.getAffiliations();
        Map<String,Map<String,Map<String,Integer>>> result = new HashMap<>();
        for(String affiliationIterator : affiliations){
            Map<String,Map<String,Integer>> affiliationResult = new HashMap<>();
            Map<String,Integer> tmp1 = new HashMap<>();
            Map<String,Integer> tmp2 = new HashMap<>();
            Map<String,Integer> tmp3 = new HashMap<>();
            Map<String,Integer> tmp4 = new HashMap<>();
            for(EchartsData programIterator : programs){
                if(affiliationIterator.equals(programIterator.getxAxis())){
                    for(EchartsDict dictIterator : leveldict){
                        if(dictIterator.getValue()==programIterator.getLevel()){
                            tmp1.put(dictIterator.getKey(),programIterator.getyAxis());
                        }
                    }
                }
            }
            affiliationResult.put("项目",tmp1);

            for(EchartsData paperIterator : papers){
                if(affiliationIterator.equals(paperIterator.getxAxis())){
                    for(EchartsDict dictIterator : leveldict){
                        if(dictIterator.getValue()==paperIterator.getLevel()){
                            tmp2.put(dictIterator.getKey(),paperIterator.getyAxis());
                        }
                    }
                }
            }
            affiliationResult.put("论文",tmp2);

            for(EchartsData patentIterator : patents){
                if(affiliationIterator.equals(patentIterator.getxAxis())){
                    for(EchartsDict dictIterator : leveldict){
                        if(dictIterator.getValue()==patentIterator.getLevel()){
                            tmp3.put(dictIterator.getKey(),patentIterator.getyAxis());
                        }
                    }
                }
            }
            affiliationResult.put("专利",tmp3);

            for(EchartsData awardIterator : awards){
                if(affiliationIterator.equals(awardIterator.getxAxis())){
                    for(EchartsDict dictIterator : leveldict){
                        if(dictIterator.getValue()==awardIterator.getLevel()){
                            tmp4.put(dictIterator.getKey(),awardIterator.getyAxis());
                        }
                    }
                }
            }
            affiliationResult.put("获奖",tmp4);

            result.put(affiliationIterator,affiliationResult);
        }
        return result;
    }
}
